<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>export命令</title>
</head>
<body>
    <h1>export命令</h1>

    <script type="module">
        // 导出变量
        export const firstName = '张';
        export const lastName = '三';
        export const year = 1990;

        // 导出函数
        export function multiply(x, y) {
            return x * y;
        }

        // 导出类
        export class Person {
            constructor(name) {
                this.name = name;
            }
        }

        // 统一导出
        const age = 18;
        const city = '北京';
        function sayHello() {
            console.log('Hello!');
        }

        export { age, city, sayHello };

        // 重命名导出
        function v1() { }
        function v2() { }

        export {
            v1 as streamV1,
            v2 as streamV2,
            v2 as streamLatestVersion
        };

        // 默认导出
        export default function () {
            console.log('默认导出的函数');
        }

        // 导出模块
        export * from './other-module.js';
        export { foo, bar } from './another-module.js';
        export { default } from './yet-another-module.js';
    </script>

    <!-- 示例模块文件 -->
    <script type="module" id="math.js">
        // math.js
        export function add(x, y) {
            return x + y;
        }
        export function subtract(x, y) {
            return x - y;
        }
        export const PI = 3.141593;
    </script>

    <script type="module" id="main.js">
        // main.js
        import { add, subtract, PI } from './math.js';
        console.log(add(2, 3)); // 5
        console.log(subtract(5, 2)); // 3
        console.log(PI); // 3.141593
    </script>
</body>
</html> 